package com.jy.adscm.operation.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.operation.purchase.bean.Bom;
import com.jy.adscm.operation.purchase.bean.QueryByPageParameter;
import com.jy.adscm.operation.purchase.mapper.mysql.BomMapper;
import com.jy.adscm.operation.purchase.service.BomService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.xjfile.bean.XjFile;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * bom清单表 服务实现类
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@Service
public class BomServiceImpl extends ServiceImpl<BomMapper, Bom> implements BomService {
@Autowired
    BomMapper bomMapper;
@Autowired
    JyCodeMapper jyCodeMapper;
    @Override
    public Page<Bom> queryByPage(QueryByPageParameter queryByPageParameter) {
        Page<Bom> page=new Page<>(queryByPageParameter.getPage(),queryByPageParameter.getRows());
        Wrapper<Bom> bomWrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryByPageParameter.getKey(),null)){
            bomWrapper.like("name",queryByPageParameter.getKey()).or()
                    .like("code",queryByPageParameter.getKey()).or()
            .like("texture",queryByPageParameter.getKey()).or()
            .like("specification",queryByPageParameter.getKey());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryByPageParameter.getSortBy(),null)){
            bomWrapper.orderBy(queryByPageParameter.getSortBy(),queryByPageParameter.getDesc());
        }

        Page<Bom> page1 = super.selectPage(page, bomWrapper);
        List<Bom> records = page1.getRecords();
        if (AdscmBaseUtils.getInstance().isNotEmpty(records,null)){
            for (Bom record : records) {
                Wrapper<JyCode> wrapper=new EntityWrapper<>();
                wrapper.eq("code_type",8);
                wrapper.eq("code",record.getManagerLevel());
                List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCodes,null)&& jyCodes.size()>0){
                    record.setManagerLevelName(jyCodes.get(0).getName());
                }
                wrapper=new EntityWrapper<>();
                wrapper.eq("code_type",9);
                wrapper.eq("code",record.getUseBelonging());
                List<JyCode> use = jyCodeMapper.selectList(wrapper);

                if (AdscmBaseUtils.getInstance().isNotEmpty(use,null)&&use.size()>0){
                    record.setUseBelongingName(use.get(0).getName());
                }

                wrapper=new EntityWrapper<>();
                wrapper.eq("code_type",10);
                wrapper.eq("code",record.getTypeAttributesName());
                List<JyCode> type = jyCodeMapper.selectList(wrapper);
                if (AdscmBaseUtils.getInstance().isNotEmpty(type,null)&&type.size()>0){
                    record.setTypeAttributesName(type.get(0).getName());
                }

            }
        }
        page1.setTotal(bomMapper.selectCount(bomWrapper));
        return page;
    }


}
